//
//  GongGaoViewController.m
//  1gene
//
//  Created by 1jiyin on 16/3/15.
//  Copyright © 2016年 1jiyin. All rights reserved.
//
//******************公告******************
#import "GongGaoViewController.h"

@interface GongGaoViewController ()<UITableViewDataSource,UITableViewDelegate>
/*
 *数据源
 */
@property(nonatomic,copy)NSMutableArray *dataArray;

@property(nonatomic,copy)UITableView *myTable;
@end

@implementation GongGaoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    _dataArray=[NSMutableArray array];
    [self.view addSubview:[UIView new]];
    [self createNavigation];
    [self createUI];
    [self getAllGongGao];
    // Do any additional setup after loading the view.
}
-(void)createNavigation
{
    self.navigationController.navigationBar.barTintColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    //返回
    UIButton *leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame=CGRectMake(0, 0, 25, 25);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"left_back"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    
    //标题
    UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
    title.text=@"今日公告";
    title.font=[UIFont systemFontOfSize:22];
    title.textAlignment=NSTextAlignmentCenter;
    title.textColor=[UIColor whiteColor];
    self.navigationItem.titleView=title;
}
//返回上一页
-(void)leftBtnClick:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)createUI
{
    UITableView *table=[[UITableView alloc]initWithFrame:CGRectMake(0, 20+44, self.view.frame.size.width, self.view.frame.size.height-44-20) style:UITableViewStylePlain];
    table.delegate=self;
    table.dataSource=self;
    table.tableHeaderView=[UIView new];
    table.tableFooterView=[UIView new];
    [self.view addSubview:table];
    _myTable=table;
}

#pragma mark tableView的代理
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    for (id subView in cell.contentView.subviews) {
        [subView removeFromSuperview];
    }
    //公告标题
    UILabel *topicLab=[[UILabel alloc]initWithFrame:CGRectMake(10, 0, self.view.frame.size.width-10-150, 44)];
    topicLab.text=_dataArray[indexPath.row][@"title"];
    topicLab.font=[UIFont systemFontOfSize:15];
    topicLab.textColor=[UIColor colorWithRed:70/255.0 green:183/255.0 blue:212/255.0 alpha:1];
    [cell.contentView addSubview:topicLab];
    
    //时间
    UILabel *time=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(topicLab.frame)+10, 0, 150, 44)];
    time.text=_dataArray[indexPath.row][@"public_time"];
    time.font=[UIFont systemFontOfSize:13];
    time.textColor=[UIColor grayColor];
    [cell.contentView addSubview:time];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GongGaoXiangQingViewController *vc=[[GongGaoXiangQingViewController alloc]init];
    if (vc.dataDic==nil) {
        vc.dataDic=[NSDictionary dictionary];
    }
    vc.dataDic=(id)_dataArray[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}

//
//-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return YES;
//}
//
//-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    [self deleteGongGao:_dataArray[indexPath.row][@"id"]];
//    [_dataArray removeObjectAtIndex:indexPath.row];
//    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
//    
//}
//-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return @"删除";
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//获取所有公告
-(void)getAllGongGao
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *token=[user objectForKey:@"token"];
    
    [manager GET:[NSString stringWithFormat:AllgongGaoMessage,token] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSArray *arr=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"公告未读消息===%@",arr);
        if (arr) {
            _dataArray=[NSMutableArray arrayWithArray:arr];
            dispatch_async(dispatch_get_main_queue(), ^{
                [_myTable reloadData];
            });
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error%@",error.localizedDescription);
    }];
    
    
}

//删除公告
-(void)deleteGongGao:(NSString *)gonggaoId
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    // [self isLoading:YES];
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *token=[user objectForKey:@"token"];
    
    [manager GET:[NSString stringWithFormat:DeleteGongGao,gonggaoId,token] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"删除公告%@",dic);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error%@",error.localizedDescription);
    }];
    
}

@end
